import java.util
        .ArrayList;
import java.util
        .List;
import java.util
        .concurrent.ExecutorService;


import java.util
        .concurrent.Executors;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author shilinghuai
 * @create 2022/4/5
 * @since 1.0.0
 */
public class Consumer {
    public static class MyCollection<T> {
        int count = 0;
        List<T> list = new ArrayList<T>();
        public synchronized void put(T t) throws InterruptedException {
                while (list.size()>=10){
                    this.wait();
                }
                count++;
                list.add(t);
                this.notifyAll();

        }
        public synchronized T get() throws InterruptedException {
            while (list.size()==0){
                  this.wait();
            }
            System.out.println("------------");
            T t
            =list.get(0);
            list.remove(0);
            this.notifyAll();
            return t;
        }
    }
    public static class Bread{

    }
    public static class ProductBread implements Runnable{
        private MyCollection<Bread> breadMyCollection;
        public ProductBread(){

        }
        public ProductBread(MyCollection<Bread> myCollection){
            this.breadMyCollection = myCollection;
        }

        @Override
        public
        void run() {
            while (true){
                Bread bread = new Bread();
                System.out.println(Thread.currentThread()+"生产面包");

                try {
                    breadMyCollection.put(bread);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }
    public static class ConsumerBread implements Runnable{
        private MyCollection<Bread> breadMyCollection;
        public ConsumerBread(){

        }
        public ConsumerBread(MyCollection<Bread> myCollection){
            this.breadMyCollection = myCollection;
        }

        @Override
        public
        void run() {
            while (true){

                try {
                    Bread bread = breadMyCollection.get();
                    System.out.println(Thread.currentThread()+"消费面包");
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args){
        MyCollection<Bread> myCollection = new MyCollection<>();
        ProductBread productBread = new ProductBread(myCollection);
        ProductBread productBread1 = new ProductBread(myCollection);
        ConsumerBread consumerBread = new ConsumerBread(myCollection);
        ConsumerBread consumerBread2 = new ConsumerBread(myCollection);
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        executorService.execute(productBread);
        executorService.execute(productBread1);
        executorService.execute(consumerBread);
        executorService.execute(consumerBread2);
    }
}
